package com.gjy.concurrency.beautiful;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-06-13 09:27:50
 */
public class ConList {
    private static final Logger log = LoggerFactory.getLogger(ConList.class);

    @Test
    public void test1() throws InterruptedException {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        TimeUnit.SECONDS.sleep(1);
        for (int i = 0; i < 1000; i++) {
            final int middle = i;
            pool.execute(() -> {
                list.remove(middle);
            });
        }

        TimeUnit.SECONDS.sleep(2);

        int size = list.size();
        log.info("list size = {}", size);
    }
}
